package com.xuelang.pipeline.infrastructure.task.repository;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuelang.hiforce.base.tracing.TracingUtils;
import com.xuelang.pipeline.domain.task.TaskInstance;
import com.xuelang.pipeline.domain.task.repository.TaskInstanceRepository;
import com.xuelang.pipeline.infrastructure.task.converter.TaskInstanceToDOConverter;
import com.xuelang.pipeline.infrastructure.task.dataobject.TaskInstanceDO;
import com.xuelang.pipeline.infrastructure.task.mapper.TaskInstanceMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>任务实例仓库实现类</p>
 *
 * @author : yixin.lys
 * @date : 2024-06-19 13:46
 **/
@Repository
public class TaskInstanceRepositoryImpl implements TaskInstanceRepository {

    @Autowired
    private TaskInstanceMapper mapper;

    @Override
    public List<TaskInstance> queryByPipelineInstanceCode(String pipelineInstanceCode) {
        LambdaQueryWrapper<TaskInstanceDO> wrapper = new LambdaQueryWrapper<>(TaskInstanceDO.class);
        wrapper.eq(StringUtils.isNotBlank(pipelineInstanceCode), TaskInstanceDO::getPipelineInstanceCode, pipelineInstanceCode);
        wrapper.orderByAsc(TaskInstanceDO::getSortIndex);
        List<TaskInstanceDO> listResult = mapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(listResult)) {
            return listResult.stream().map(TaskInstanceToDOConverter.INSTANCE::fromDO).collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public TaskInstance queryByTaskUuid(String taskUuid, String pipelineInstanceCode) {
        LambdaQueryWrapper<TaskInstanceDO> wrapper = new LambdaQueryWrapper<>(TaskInstanceDO.class);
        wrapper.eq(StringUtils.isNotBlank(taskUuid), TaskInstanceDO::getTaskUuid, taskUuid);
        wrapper.eq(StringUtils.isNotBlank(pipelineInstanceCode), TaskInstanceDO::getPipelineInstanceCode, pipelineInstanceCode);
        TaskInstanceDO taskInstanceDO = mapper.selectOne(wrapper);
        return TaskInstanceToDOConverter.INSTANCE.fromDO(taskInstanceDO);
    }

    @Override
    public List<String> queryBuildOutput(String pipelineInstanceCode, String taskInstanceCode) {
        LambdaQueryWrapper<TaskInstanceDO> wrapper = new LambdaQueryWrapper<>(TaskInstanceDO.class);
        wrapper.eq(StringUtils.isNotBlank(taskInstanceCode), TaskInstanceDO::getCode, taskInstanceCode);
        wrapper.eq(StringUtils.isNotBlank(pipelineInstanceCode), TaskInstanceDO::getPipelineInstanceCode, pipelineInstanceCode);
        List<TaskInstanceDO> taskInstanceDOList = mapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(taskInstanceDOList)) {
            return taskInstanceDOList.stream().map(TaskInstanceDO::getOutput).collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public TaskInstance queryByTaskCode(String taskInstanceCode) {
        LambdaQueryWrapper<TaskInstanceDO> wrapper = new LambdaQueryWrapper<>(TaskInstanceDO.class);
        wrapper.eq(StringUtils.isNotBlank(taskInstanceCode), TaskInstanceDO::getCode, taskInstanceCode);
        TaskInstanceDO taskInstanceDO = mapper.selectOne(wrapper);
        return TaskInstanceToDOConverter.INSTANCE.fromDO(taskInstanceDO);
    }
    private String getUserId(String userId) {
        String getUserId = TracingUtils.getUserId();
        return StrUtil.isBlank(getUserId) ? "-1" : getUserId;
    }

    @Override
    public void add(TaskInstance taskInstance) {
        mapper.insert(TaskInstanceToDOConverter.INSTANCE.toDO(taskInstance));
    }

    @Override
    public void update(TaskInstance taskInstance) {
        mapper.updateById(TaskInstanceToDOConverter.INSTANCE.toDO(taskInstance));
    }

    @Override
    public void removeByIds(List<String> idList) {
        mapper.deleteBatchIds(idList);
    }

    @Override
    public void removeByPipelineInstanceCodeList(List<String> pipelineInstanceCodeList) {
        LambdaQueryWrapper<TaskInstanceDO> wrapper = new LambdaQueryWrapper<>(TaskInstanceDO.class);
        wrapper.in(TaskInstanceDO::getPipelineInstanceCode, pipelineInstanceCodeList);
        mapper.delete(wrapper);
    }

    @Override
    public void removeByTaskUuidList(List<String> taskUuidList) {
        LambdaQueryWrapper<TaskInstanceDO> wrapper = new LambdaQueryWrapper<>(TaskInstanceDO.class);
        wrapper.in(TaskInstanceDO::getTaskUuid, taskUuidList);
        mapper.delete(wrapper);
    }
}
